package com.tyut.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.tyut.pojo.Collect;
import com.tyut.pojo.Travel;
import com.tyut.service.CollectService;
import com.tyut.service.TravelService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.LinkedList;
import java.util.List;



/**
 * 旅游景点控制器
 */
@Controller
public class TravelController {

    @Resource
    private TravelService travelService;
    /**
     * 查询所有景区数据，返回给前端
     * @return
     */
    @GetMapping(value = "/queryListTravel")
    @ResponseBody
    public List<Travel> queryListTravel(HttpServletResponse response){
        response.setHeader("Access-Control-Allow-Origin","*");
        List<Travel> travels = travelService.queryListTravel();
        System.out.println("debug==>travels"+travels);
        return travels;
    }




    /**
     * 查根据id查询数据给前端
     * @return
     */
    @GetMapping(value = "/queryTravelById/{id}")
    @ResponseBody
    public Travel queryTravelById(@PathVariable("id") Integer id, HttpServletResponse response){
        Travel travel = travelService.queryTravelById(id);
        ObjectMapper objectMapper = new ObjectMapper();
        response.setHeader("Access-Control-Allow-Origin","*");
        return travel;

    }
    /**
     * 根据标签查询数据返回数据给前端
     * @return
     */
    @GetMapping(value = "/queryListTravelByLabel/{label}")
    @ResponseBody
    public List<Travel> queryListTravelByLabel(@PathVariable("label") Integer label, HttpServletResponse response){
        List<Travel> travel = travelService.queryListTravelByLabel(label);
        System.out.println(travel);
        response.setHeader("Access-Control-Allow-Origin","*");
        return travel;
    }
//添加景点
    @PostMapping("/addTravel")
    public void addTravel(Travel travel) {
        travelService.addTravel(travel);
    }
//删除景点
    @DeleteMapping("/deleteTravel/{id}")
    public void deleteTravel(@PathVariable("tid") Integer tid) {
        travelService.deleteTravel(tid);
    }
//修改景点
    @PostMapping("/updateTravel")
    public void updateTravel(Travel travel) {
        travelService.updateTravel(travel);
    }


    @Resource
    private CollectService collectService;

    /**
     * 查询用户收藏的景点
     * 根据用户的 id 查找景点的id，
     * 然后根据景点id返回景点数据
     * @param cid
     * @return
     */
    @GetMapping(value = "/queryTravelIdByCustomerId/{cid}")
    @ResponseBody
    public List<Travel> queryTravelIdByCustomerId(@PathVariable("cid") Integer cid,HttpServletResponse response){
        response.setHeader("Access-Control-Allow-Origin","*");
        List<Integer> tids = collectService.queryTravelIdByCustomerId(cid);
        List<Travel> travels = new LinkedList<Travel>();
        for (Integer tid : tids) {
            travels.add(travelService.queryTravelById(tid));
        }
        return travels;
    }

    /**
     * 删除用户收藏的景点
     * @param cid
     * @param tid
     * @param response
     * @return
     */
    @RequestMapping("/deleteCollectById/{cid}/{tid}")
    @ResponseBody
    public boolean deleteCollectById(@PathVariable("cid") int cid, @PathVariable("tid") int tid,HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin","*");
        Collect collect = new Collect();
        collect.setCid(cid);
        collect.setTid(tid);
        return collectService.deleteCollectById(collect);
    }

    /**
     * 添加用户收藏的景点
     * @param cid,tid (用户 id + 景区 id)
     */
    @GetMapping("/addCollect/{cid}/{tid}")
    @ResponseBody
    public boolean addCollect(@PathVariable("cid") int cid, @PathVariable("tid") int tid,HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin","*");
        Collect collect = new Collect();
        collect.setCid(cid);
        collect.setTid(tid);
        List<Integer> tids = collectService.queryTravelIdByCustomerId(cid);
        for(Integer i : tids){
            if(i == tid) return true;
        }
        return collectService.addCollect(collect);
    }



}
